* Purpose: To study the intervention strength for info_study
* Author: Sadish
* Last Modified: 2021-04-22
* Notes:

version 13

* Inputs **************************************************************
  local data "${is_root}/6_Processed_Data/is_4_4_6_publicData.dta"
  local intetactions "${is_root}/7_Output/is_4_4_5_intetactionsTable"
  local output "${is_root}/7_Output/is_4_4_5_output"
  local uid "empcode" // variables to uniquely identify individuals
***********************************************************************


clear all
set more off
set mat 10000
set scheme lean1 // plottig
tempfile box

pause on


* Get Clean Data ******************************************************
use "`data'", clear
save `box', replace
***********************************************************************

* Interactions ********************************************************
gen audioXnumeracy_hi = audio*numeracy_hi
  la var audioXnumeracy_hi "Audio Message X High Numeracy"
gen audioXmemory_hi = audio*memory_hi
  la var audioXmemory_hi "Audio Message X High Memory"
gen audioXsmartphone = audio*smartphone
  la var audioXsmartphone "Audio Message X Smartphone"

gen PCXnumeracy_hi = PC*numeracy_hi
  la var PCXnumeracy_hi "Phone Call X High Numeracy"
gen PCXmemory_hi = PC*memory_hi
  la var PCXmemory_hi "Phone Call X High Memory"
gen PCXsmartphone = PC*smartphone
  la var PCXsmartphone "Phone Call X Smartphone"
***********************************************************************

* Regressions *********************************************************
{
local FE "round surveyor_bl surveyor_el"
* Code might assume that numeracy_hi, memory_hi, and smartphone
*   are included in control variables.
local X "age female edu_hi quit hostel numeracy_hi memory_hi smartphone"
local vce "robust"
local outcomes "knowledge distress"
local specs = 1 // number of specifications for each outcome


// Tables ***************************************************
* * * * * * * * * * * * *
  di "This assersion is in is_4_4_5 file"
  di "Assert that numeracy_hi, memory_hi, and smartphone are listed as control variables"
  assert strpos("`X'", "numeracy_hi")!=0
  assert strpos("`X'", "memory_hi")!=0
  assert strpos("`X'", "smartphone")!=0
* * * * * * * * * * * * *

local heading ""
local counter "Variables"
local b_PC: var lab PC
local se_PC ""

local b_PCn: var lab PCXnumeracy_hi
local se_PCn ""
local b_PCm: var lab PCXmemory_hi
local se_PCm ""
local b_PCs: var lab PCXsmartphone
local se_PCs ""

local b_n: var lab numeracy_hi
local se_n ""
local b_m: var lab memory_hi
local se_m ""
loca b_s: var lab smartphone
local se_s ""

foreach v in `outcomes' {
  local b_`v'_bl: var lab `v'_bl
    local b_`v'_bl = "`b_`v'_bl'" + " (\textit{Bl})"
  local se_`v'_bl ""
}
local sblFE "Surveyor (\textit{Bl}) \textit{FE}"
local selFE "Surveyor (\textit{Bl}) \textit{FE}"
local rFE "Trial Round \textit{FE}"
local ipw "\textit{IPW}"
local ctrl "Control Variables"

local N "Observations"
local ar2 "Adjusted \(R^2\)" // Latex perfers \()\ here rather than $$
local mean "Mean of Outcome (\textit{Bl})"

  local cols=0 // column counter for table
  foreach y in `outcomes' {
  forvalues i=1(1)`specs' {
	// Column counter:
	local cols = `cols' + 1
	local counter "`counter' & (`cols')"
	if `i'==1 {
	  reghdfe `y'_el PC PCXnumeracy_hi PCXmemory_hi PCXsmartphone `y'_bl `X' [pweight = ipw], ///
	    a(`FE') vce(`vce')
	  
	  local b_audio "`b_audio' & "
	  local se_audio "`se_audio' & "
	  
	  local coef_PC = string(_b[PC], "%12.2fc")
	  local b_PC "`b_PC' & `coef_PC'"
	  local err_PC = "(" + string(_se[PC], "%12.2fc") + ")"
	  local se_PC "`se_PC' & `err_PC'"
	  
	  local coef_PCn = string(_b[PCXnumeracy_hi], "%12.2fc")
	  local b_PCn "`b_PCn' & `coef_PCn'"
	  local err_PCn = "(" + string(_se[PCXnumeracy_hi], "%12.2fc") + ")"
	  local se_PCn "`se_PCn' & `err_PCn'"
	  
	  local coef_PCm = string(_b[PCXmemory_hi], "%12.2fc")
	  local b_PCm "`b_PCm' & `coef_PCm'"
	  local err_PCm = "(" + string(_se[PCXmemory_hi], "%12.2fc") + ")"
	  local se_PCm "`se_PCm' & `err_PCm'"
	  
	  local coef_PCs = string(_b[PCXsmartphone], "%12.2fc")
	  local b_PCs "`b_PCs' & `coef_PCs'"
	  local err_PCs = "(" + string(_se[PCXsmartphone], "%12.2fc") + ")"
	  local se_PCs "`se_PCs' & `err_PCs'"
	  
	  local coef_n = string(_b[numeracy_hi], "%12.2fc")
	  local b_n "`b_n' & `coef_n'"
	  local err_n = "(" + string(_se[numeracy_hi], "%12.2fc") + ")"
	  local se_n "`se_n' & `err_n'"
	  
	  local coef_m = string(_b[memory_hi], "%12.2fc")
	  local b_m "`b_m' & `coef_m'"
	  local err_m = "(" + string(_se[memory_hi], "%12.2fc") + ")"
	  local se_m "`se_m' & `err_m'"
	  
	  local coef_s = string(_b[smartphone], "%12.2fc")
	  local b_s "`b_s' & `coef_s'"
	  local err_s = "(" + string(_se[smartphone], "%12.2fc") + ")"
	  local se_s "`se_s' & `err_s'"

	  foreach v in `outcomes' {
	  capture: di _b[`v'_bl] // see if variable is regressor
	  if _rc==0 {
	    local coef_bl = string(_b[`v'_bl], "%12.2fc")
		local b_`v'_bl "`b_`v'_bl' & `coef_bl'"
		local err_bl = "(" + string(_se[`v'_bl], "%12.2fc") + ")"
		local se_`v'_bl "`se_`v'_bl' & `err_bl'"
	  }
	  else {
		local b_`v'_bl "`b_`v'_bl' & "
		local se_`v'_bl "`se_`v'_bl' & "
	  }
	  }
	  
	  local sblFE "`sblFE' & \(\checkmark\)"
	  local selFE "`selFE' & \(\checkmark\)"
	  local rFE "`rFE' & \(\checkmark\)"
	  local ipw "`ipw' & \(\checkmark\)"
	  local ctrl "`ctrl' & \(\checkmark\)"
	}
// Observations:
	local obs = string(e(N), "%12.0fc")
	local N "`N' & `obs'"
	// R squared:
	local adjr2 = string(e(r2_a), "%12.2fc")
	local ar2 "`ar2' & `adjr2'"
  }
  
  // Table headings row:
  local `y'lab: var lab `y'_bl
  local heading "`heading' & \multicolumn{`specs'}{c}{``y'lab'}"
  
  // Mean of Dependent Variable:
  su `y'_bl
  local depmean = string(r(mean), "%12.2fc")
  local mean "`mean' & \multicolumn{`specs'}{c}{`depmean'}"
  }
  
  // Latex table:
  cap: file close texFile
  file open texFile using "`intetactions'.tex", write replace
  file write texFile ///
    "\begin{tabular}{a{5.4cm} *{`cols'}{b{1.6cm}}} \toprule % The *{#} repeats the following argument for # columns. a{} is ragged right column, b{} is centered." _n
  file write texFile " `heading' \\" _n
  file write texFile " `counter' \\ \midrule" _n
  
  file write texFile " `b_PC' \\" _n
  file write texFile " `se_PC' \\" _n
  
  file write texFile " `b_PCn' \\" _n
  file write texFile " `se_PCn' \\" _n
  file write texFile " `b_PCm' \\" _n
  file write texFile " `se_PCm' \\" _n  
  file write texFile " `b_PCs' \\" _n
  file write texFile " `se_PCs' \\" _n  
  
  file write texFile " `b_n' \\" _n
  file write texFile " `se_n' \\" _n
  file write texFile " `b_m' \\" _n
  file write texFile " `se_m' \\" _n
  file write texFile " `b_s' \\" _n
  file write texFile " `se_s' \\" _n
  
  foreach v in `outcomes' {
    file write texFile " `b_`v'_bl' \\" _n
    file write texFile " `se_`v'_bl' \\" _n
  }
  
  file write texFile " `sblFE' \\" _n
  file write texFile " `selFE' \\" _n
  file write texFile " `rFE' \\" _n
  file write texFile " `ipw' \\" _n
  file write texFile " `ctrl' \\" _n
  file write texFile " `N' \\" _n
  file write texFile " `ar2' \\ % Stata perfers \()\ here rather than $$" _n
  file write texFile " `mean' \\ \bottomrule" _n
  file write texFile "\end{tabular}" _n
  file close texFile
***********************************************************************
}
//

* Pre-Registered ******************************************************
{
local FE "round surveyor_bl surveyor_el"
* Code might assume that numeracy_hi, memory_hi, and smartphone
*   are included in control variables.
local X "age female edu_hi quit numeracy_hi memory_hi smartphone hostel"
local vce "robust"
local outcomes "knowledge distress"
local specs = 1 // number of specifications for each outcome


// Tables ***************************************************
* * * * * * * * * * * * *
  di "This assersion is in is_4_4_5 file"
  di "Assert that numeracy_hi, memory_hi, and smartphone are listed as control variables"
  assert strpos("`X'", "numeracy_hi")!=0
  assert strpos("`X'", "memory_hi")!=0
  assert strpos("`X'", "smartphone")!=0
* * * * * * * * * * * * *

local heading ""
local counter "Variables"
local b_audio: var lab audio
local se_audio ""

local b_an: var lab audioXnumeracy_hi
local se_an ""
local b_am: var lab audioXmemory_hi
local se_am ""
local b_as: var lab audioXsmartphone
local se_as ""

local b_n: var lab numeracy_hi
local se_n ""
local b_m: var lab memory_hi
local se_m ""
loca b_s: var lab smartphone
local se_s ""

foreach v in `outcomes' {
  local b_`v'_bl: var lab `v'_bl
    local b_`v'_bl = "`b_`v'_bl'" + " (\textit{Bl})"
  local se_`v'_bl ""
}
local sblFE "Surveyor (\textit{Bl}) \textit{FE}"
local selFE "Surveyor (\textit{Bl}) \textit{FE}"
local rFE "Trial Round \textit{FE}"
local ipw "\textit{IPW}"
local ctrl "Control Variables"

local N "Observations"
local ar2 "Adjusted \(R^2\)" // Latex perfers \()\ here rather than $$
local mean "Mean of Outcome (\textit{Bl})"

  local cols=0 // column counter for table
  foreach y in `outcomes' {
  forvalues i=1(1)`specs' {
	// Column counter:
	local cols = `cols' + 1
	local counter "`counter' & (`cols')"
	if `i'==1 {
	  reghdfe `y'_el audio audioXnumeracy_hi audioXmemory_hi audioXsmartphone `y'_bl `X' [pweight = ipw], ///
	    a(`FE') vce(`vce')
	  
	  local coef_audio = string(_b[audio], "%12.2fc")
	  local b_audio "`b_audio' & `coef_audio'"
	  local err_audio = "(" + string(_se[audio], "%12.2fc") + ")"
	  local se_audio "`se_audio' & `err_audio'"
	  
	  local coef_an = string(_b[audioXnumeracy_hi], "%12.2fc")
	  local b_an "`b_an' & `coef_an'"
	  local err_an = "(" + string(_se[audioXnumeracy_hi], "%12.2fc") + ")"
	  local se_an "`se_an' & `err_an'"
	  
	  local coef_am = string(_b[audioXmemory_hi], "%12.2fc")
	  local b_am "`b_am' & `coef_am'"
	  local err_am = "(" + string(_se[audioXmemory_hi], "%12.2fc") + ")"
	  local se_am "`se_am' & `err_am'"
	  
	  local coef_as = string(_b[audioXsmartphone], "%12.2fc")
	  local b_as "`b_as' & `coef_as'"
	  local err_as = "(" + string(_se[audioXsmartphone], "%12.2fc") + ")"
	  local se_as "`se_as' & `err_as'"
	  
	  local coef_n = string(_b[numeracy_hi], "%12.2fc")
	  local b_n "`b_n' & `coef_n'"
	  local err_n = "(" + string(_se[numeracy_hi], "%12.2fc") + ")"
	  local se_n "`se_n' & `err_n'"
	  
	  local coef_m = string(_b[memory_hi], "%12.2fc")
	  local b_m "`b_m' & `coef_m'"
	  local err_m = "(" + string(_se[memory_hi], "%12.2fc") + ")"
	  local se_m "`se_m' & `err_m'"
	  
	  local coef_s = string(_b[smartphone], "%12.2fc")
	  local b_s "`b_s' & `coef_s'"
	  local err_s = "(" + string(_se[smartphone], "%12.2fc") + ")"
	  local se_s "`se_s' & `err_s'"

	  foreach v in `outcomes' {
	  capture: di _b[`v'_bl] // see if variable is regressor
	  if _rc==0 {
	    local coef_bl = string(_b[`v'_bl], "%12.2fc")
		local b_`v'_bl "`b_`v'_bl' & `coef_bl'"
		local err_bl = "(" + string(_se[`v'_bl], "%12.2fc") + ")"
		local se_`v'_bl "`se_`v'_bl' & `err_bl'"
	  }
	  else {
		local b_`v'_bl "`b_`v'_bl' & "
		local se_`v'_bl "`se_`v'_bl' & "
	  }
	  }
	  
	  local sblFE "`sblFE' & \(\checkmark\)"
	  local selFE "`selFE' & \(\checkmark\)"
	  local rFE "`rFE' & \(\checkmark\)"
	  local ipw "`ipw' & \(\checkmark\)"
	  local ctrl "`ctrl' & \(\checkmark\)"
	}

	// Observations:
	local obs = string(e(N), "%12.0fc")
	local N "`N' & `obs'"
	// R squared:
	local adjr2 = string(e(r2_a), "%12.2fc")
	local ar2 "`ar2' & `adjr2'"
  }
  
  // Table headings row:
  local `y'lab: var lab `y'_bl
  local heading "`heading' & \multicolumn{`specs'}{c}{``y'lab'}"
  
  // Mean of Dependent Variable:
  su `y'_bl
  local depmean = string(r(mean), "%12.2fc")
  local mean "`mean' & \multicolumn{`specs'}{c}{`depmean'}"
  }
  
  // Latex table:
  cap: file close texFile
  file open texFile using "`intetactions'_prereg.tex", write replace
  file write texFile ///
    "\begin{tabular}{a{5.4cm} *{`cols'}{b{1.6cm}}} \toprule % The *{#} repeats the following argument for # columns. a{} is ragged right column, b{} is centered." _n
  file write texFile " `heading' \\" _n
  file write texFile " `counter' \\ \midrule" _n
  
  file write texFile " `b_audio' \\" _n
  file write texFile " `se_audio' \\" _n
  
  file write texFile " `b_an' \\" _n
  file write texFile " `se_an' \\" _n
  file write texFile " `b_am' \\" _n
  file write texFile " `se_am' \\" _n
  file write texFile " `b_as' \\" _n
  file write texFile " `se_as' \\" _n
  
  file write texFile " `b_n' \\" _n
  file write texFile " `se_n' \\" _n
  file write texFile " `b_m' \\" _n
  file write texFile " `se_m' \\" _n
  file write texFile " `b_s' \\" _n
  file write texFile " `se_s' \\" _n
  
  foreach v in `outcomes' {
    file write texFile " `b_`v'_bl' \\" _n
    file write texFile " `se_`v'_bl' \\" _n
  }
  
  file write texFile " `sblFE' \\" _n
  file write texFile " `selFE' \\" _n
  file write texFile " `rFE' \\" _n
  file write texFile " `ipw' \\" _n
  file write texFile " `ctrl' \\" _n
  file write texFile " `N' \\" _n
  file write texFile " `ar2' \\ % Stata perfers \()\ here rather than $$" _n
  file write texFile " `mean' \\ \bottomrule" _n
  file write texFile "\end{tabular}" _n
  file close texFile
***********************************************************************
}
//
